<?php

/**
 *
 * @author Zsolt Lengyel
 *
 */
class RegistrationHelper{

   /**
    *
    * @var Registration
    */
   private $registration;

   /**
    *
    * @param Registration $registration ezt kell lekezelni. Ez már mentve van db-be
    */
   public function __construct(Registration $registration){
      $this->registration = $registration;
   }


   /**
    *
    * @return boolean true if perform was successful
    */
   public function perform(){




      /* @var $mailer PHPMailer */
      $mailer = Yii::app()->mailer;


      if(YII_DEVEL){

         header('Content-Type: text/html; charset=utf-8');
          $mailer->getView('registration', array('model'=>$this->registration, 'title'=>'Kurzus regisztráció.'), 'base');

          echo $mailer->Body;
          Yii::app()->end();

          return true;
      }


      $mailer->SetLanguage('hu');

      $mailer->CharSet = 'UTF-8';


      // set connection
      $mailer->IsSendmail();

      /*

      $smtp = Yii::app()->params['smtp'];
      $mailer->Host       = $smtp['server']; // SMTP server
      $mailer->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                                    // 1 = errors and messages
                                                    // 2 = messages only
      $mailer->SMTPAuth   = true;                  // enable SMTP authentication
      $mailer->Host       = $smtp['server']; // sets the SMTP server
      $mailer->Port       = $smtp['port'];                    // set the SMTP port for the GMAIL server
      $mailer->Username   = $smtp['user']; // SMTP account username
      $mailer->Password   = $smtp['password'];        // SMTP account password
      */

      $mailer->IsHTML();

      $mailer->From = Yii::app()->params['registraionEmail'];
      $mailer->FromName = Yii::app()->name;

      $mailer->Subject = 'Regisztráció aktiválás';
      $mailer->AddReplyTo(Yii::app()->params['registraionEmail'], Yii::app()->name);

      $mailer->AddAddress($this->registration->email, $this->registration->getFullname());
      $mailer->AddCC(Yii::app()->params['registraionEmail'], Yii::app()->name);

      // set the content
      $mailer->getView('registration', array('model'=>$this->registration, 'title'=>'Kurzus regisztráció.'), 'base');

      ob_start();
      $result = $mailer->Send();
      $output = ob_get_clean();
      ob_end_clean();

      if(!$result){

         if(!empty($output)){
            Yii::app()->user->setFlash('error',$output);
         }

         Yii::app()->user->setFlash('error', 'Hiba történt az aktivációs e-mail külése közben.');

      }

      return $result;

   }

   /**
    *
    * @param integer $length length of random
    * @return string random string
    */
   private function generateRandomString($length = 64) {
       $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       $randomString = '';
       for ($i = 0; $i < $length; $i++) {
           $randomString .= $characters[rand(0, strlen($characters) - 1)];
       }
       return $randomString;
   }


}